package com.cci.kangdao.controller.shankang;

import com.cci.kangdao.controller.shankang.service.ShankangService;
import com.cci.kangdao.dao.ExternalAuthMapper;
import com.cci.kangdao.dao.model.ExternalAuthInfo;
import com.cci.kangdao.dto.BaseResponse;
import com.cci.kangdao.dto.RoadTestHeader;
import com.gexin.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @description:
 * @author: joe
 * @company: CTY
 * @since: 2024/1/18 17:39
 * @version: V1.0
 **/
@RestController
@RequestMapping("shanKang")
public class ShankangController {

    private Logger log = Logger.getLogger(this.getClass().getName());
    @Autowired
    private ShankangService shankangService;
    @Resource
    private ExternalAuthMapper externalAuthMapper;

    /** 
     * 添加用户
     * @param userStr
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "saveUser.aspx",method = RequestMethod.POST)
    public BaseResponse saveUser(@RequestBody String userStr, HttpServletRequest request, HttpServletResponse response,@RequestHeader Map<String, String> headerMap){
        log.error("saveUser.aspx请求参数："+userStr);
        //验证token
        String tokenStr = verifyToken(headerMap);
        if (StringUtils.isNotBlank(tokenStr)) {
            return BaseResponse.error(tokenStr);
        }
        return shankangService.saveUser(userStr,request,response);
    }

    /**
     * 禁用启用用户      启用时需要陕康云重新同步车辆车多数据
     * @param userStr
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "disableOrStartUser.aspx",method = RequestMethod.POST)
    public BaseResponse disableOrStartUser(@RequestBody String userStr, HttpServletRequest request, HttpServletResponse response,@RequestHeader Map<String, String> headerMap){
        log.error("disableOrStartUser.aspx请求参数："+userStr);
        //验证token
        String tokenStr = verifyToken(headerMap);
        if (StringUtils.isNotBlank(tokenStr)) {
            return BaseResponse.error(tokenStr);
        }
        return shankangService.disableOrStartUser(userStr,response);
    }

    /**
     * 更新用户
     * @param userStr
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "updateUser.aspx",method = RequestMethod.POST)
    public BaseResponse updateUser(@RequestBody String userStr, HttpServletRequest request, HttpServletResponse response,@RequestHeader Map<String, String> headerMap){
        log.error("updateUser.aspx请求参数："+userStr);
        //验证token
        String tokenStr = verifyToken(headerMap);
        if (StringUtils.isNotBlank(tokenStr)) {
            return BaseResponse.error(tokenStr);
        }
        return shankangService.updateUser(userStr,response,request);
    }


    /**
     * 上传文件维护车辆
     * @return
     */
    @RequestMapping(value = "upholdVehicles.aspx",method = RequestMethod.POST)
    public BaseResponse upholdVehicles(@RequestParam("file") MultipartFile file, @RequestHeader Map<String, String> headerMap){

        try {
            return shankangService.upholdVehicles(file);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return BaseResponse.error("系统错误");
        }

    }

    private String verifyToken(Map<String, String> headerMap){
        //验证token
        RoadTestHeader header = JSON.parseObject(JSON.toJSONString(headerMap), RoadTestHeader.class);
        log.info(  "header信息:"+header);
        if(null == header){
            return "{\"status\": -99,\"msg\": \"缺少token参数\"}";
        }
        if(StringUtils.isBlank(header.getSource()) || StringUtils.isBlank(header.getVersion()) || StringUtils.isBlank(header.getToken()) || StringUtils.isBlank(header.getSystemTime())){
            return "{\"status\": -99,\"msg\": \"缺少token参数\"}";
        }
        ExternalAuthInfo authInfo = externalAuthMapper.getAuthBySource(header.getSource(),header.getVersion());
        if(null == authInfo){
            return "{\"status\": -99,\"msg\": \"未查询到token信息\"}";
        }
        if(!authInfo.getToken().equals(header.getToken())){
            return "{\"status\": -99,\"msg\": \"token验证失败\"}";
        }
        return null;
    }
}
